import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_form_core/business_components/base_request_form_container.dart';
import 'package:x3_form_core/business_select_components/user_select_page.dart';
import 'package:x3_form_core/business_select_components/vip_select_page.dart';
import 'package:x3_form_core/models/user_select_model.dart';
import 'package:x3_form_core/models/vip_select_model.dart';
import 'package:x3_form_core/provider/base_request_form_provider.dart';

/// author：william on 2021/3/31 17:47
/// email：362661044@qq.com

class BaseRequestFormBusinessSelect<T extends BaseKeyValueModel> extends StatefulWidget {
  final BaseHttpRequestModel request;
  final RequestSaveData requestSaveData;
  final bool isMulti;

  const BaseRequestFormBusinessSelect({Key key, this.isMulti = false, @required this.request, @required this.requestSaveData}) : super(key: key);
  @override
  _BaseRequestFormBusinessSelectState createState() => _BaseRequestFormBusinessSelectState<T>();
}

class _BaseRequestFormBusinessSelectState<T extends BaseKeyValueModel> extends State<BaseRequestFormBusinessSelect> {
  BaseHttpRequestModel get request => widget.request;

  BaseRequestFormProvider _baseRequestFormProvider;

  /// 构建文本
  String get _textString {
    var data = _baseRequestFormProvider.getDataSource(request);
    if (data is Map) return '[${data['key']}] ${data['value']}';
    if (data is List) {
      return data.map((e) => '[${e['key']}] ${e['value']}').toList().join('，');
    }
    return '';
  }

  List<BaseKeyValueModel> initData() {
    var data = _baseRequestFormProvider.getDataSource(request);
    if (data is Map) return [BaseKeyValueModel.fromJson(data)];
    if (data is List) {
      return data.map((e) => BaseKeyValueModel.fromJson(e)).toList();
    }
    return [];
  }

  @override
  Widget build(BuildContext context) {
    _baseRequestFormProvider = BaseRequestFormProvider.of(context);
    return BaseRequestFormContainer(
      request: request,
      showSuffixIcon: !_baseRequestFormProvider.isReadonly(widget.request),
      onPress: _onSelectData,
      child: _buildText(),
    );
  }

  /// 构建文本值
  Widget _buildText() {
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: _textString.isNotEmpty ? Text(_textString) : Text(request.notes, style: Theme.of(context).inputDecorationTheme.hintStyle),
    );
  }

  /// 点击选择事件
  void _onSelectData() {
    Widget child = _buildSelectPage();
    if (child != null) {
      BaseRouterUtil().push(child).then((value) {
        ///单选
        if (value is BaseKeyValueModel) {
          _baseRequestFormProvider.setDataSource(request, value.toBaseJson());
          setState(() {});
        }

        /// 多选
        if (value is List<BaseKeyValueModel>) {
          _baseRequestFormProvider.setDataSource(request, value.map((e) => e.toBaseJson()).toList());
          setState(() {});
        }
      });
    }
  }

  /// 注册选择器页面
  Widget _buildSelectPage() {
    if (T == UserSelectModel) return UserSelectPage(isMulti: widget.isMulti, request: request, initData: initData());
    if (T == VipSelectModel) return VipSelectPage(isMulti: widget.isMulti, request: request, initData: initData());
    return null;
  }
}
